package com.fsts.server;

import java.math.BigInteger;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import com.fsts.entity.PrivateKey;
import com.fsts.entity.PublicKey;
import com.fsts.util.JDBCUtil;

/**
 * TC与数据库访问层
 * 
 * 
 * @version 1.0
 * @since JDK1.6(建议)
 */
public class TCDao {

	/**
	 * 添加publickey
	 */
	public void addPublicKey(PublicKey plk) {
		Connection con = null;
		Statement stmt = null;
		ResultSet rs = null;
		try {
			// 2.连数据库
			con = JDBCUtil.getConn();
			// 3.获取数据库操作对象(SQL)
			stmt = con.createStatement();
			// 4.操作数据库(查询)
			String sql = "insert into public(public_n,public_e,public_g) values('"
					+ plk.getN()
					+ "','"
					+ plk.getE()
					+ "','"
					+ plk.getG()
					+ "')";
			System.out.println(sql);
			stmt.execute(sql);
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("操作失败");
		} finally {
			// 6.关闭JDBC对象
			JDBCUtil.close(rs, stmt, con);
		}
	}

	/**
	 * 添加privateKey
	 * 
	 * @param pek
	 */
	public void addPrivateKey(PrivateKey pek) {
		Connection con = null;
		Statement stmt = null;
		ResultSet rs = null;
		try {
			// 2.连数据库
			con = JDBCUtil.getConn();
			// 3.获取数据库操作对象(SQL)
			stmt = con.createStatement();
			// 4.操作数据库(查询)
			String sql = "insert into private(private_p,private_q,private_d) values('"
					+ pek.getP()
					+ "','"
					+ pek.getQ()
					+ "','"
					+ pek.getD()
					+ "')";
			System.out.println(sql);
			stmt.execute(sql);
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("操作失败");
		} finally {
			// 6.关闭JDBC对象
			JDBCUtil.close(rs, stmt, con);
		}
	}

	/**
	 * 获取PublicKey
	 * 
	 * @return
	 */
	public PublicKey getPublicKey() {
		PublicKey plk = new PublicKey();
		Connection con = null;
		Statement stmt = null;
		ResultSet rs = null;
		try {
			// 2.连数据库
			con = JDBCUtil.getConn();
			// 3.获取数据库操作对象(SQL)
			stmt = con.createStatement();
			// 4.操作数据库(查询)
			String sql = "select * from public where public_id = (select max(public_id) from public)";
			rs = stmt.executeQuery(sql);
			if (rs.next()) {
				plk.setId(rs.getInt("public_id"));
				plk.setN(new BigInteger(rs.getString("public_n")));
				plk.setE(new BigInteger(rs.getString("public_e")));
				plk.setG(new BigInteger(rs.getString("public_g")));
			}
		} catch (Exception e) {
			// e.printStackTrace();
			System.out.println("操作失败");
		} finally {
			// 6.关闭JDBC对象
			JDBCUtil.close(rs, stmt, con);
		}
		return plk;
	}

	/**
	 * 获取PrivateKey
	 * 
	 * @return
	 */
	public PrivateKey getPrivateKey() {
		PrivateKey pek = new PrivateKey();
		;
		Connection con = null;
		Statement stmt = null;
		ResultSet rs = null;
		try {
			// 2.连数据库
			con = JDBCUtil.getConn();
			// 3.获取数据库操作对象(SQL)
			stmt = con.createStatement();
			// 4.操作数据库(查询)
			String sql = "select * from private where private_id = (select max(private_id) from private)";
			rs = stmt.executeQuery(sql);
			if (rs.next()) {
				pek.setId(rs.getInt("private_id"));
				pek.setP(new BigInteger(rs.getString("private_p")));
				pek.setQ(new BigInteger(rs.getString("private_q")));
				pek.setD(new BigInteger(rs.getString("private_d")));
			}
		} catch (Exception e) {
			System.out.println("操作失败");
		} finally {
			JDBCUtil.close(rs, stmt, con);
		}

		return pek;
	}

}
